/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Richo
 */
public class onlyLetter {
    public onlyLetter() {
    }

    /** methode ini dipakai untuk mengeset dan mendapatkan filter
     *  @return mengembalikan filter bertype PlainDocument
     *  Menyaring Hanya karakter huruf (Letter) yang bisa dimasukkan
     */
    public PlainDocument getOnlyLetter() {
      PlainDocument filterLetter = new PlainDocument() {
        public void insertString(int offs, String str, AttributeSet a) throws
            BadLocationException {
          StringBuffer buffer = new StringBuffer();
          int s = 0;
          char[] dataInput = str.toCharArray();
          // Memeriksa semua data yang dimasukkan
          for (int i = 0; i < dataInput.length; i++) {
            // Menyaring Apakah data masukkan berupa LETTER ??
            boolean isOnlyLetter = Character.isLetter(dataInput[i]);
            if (isOnlyLetter == true) {
              dataInput[s] = dataInput[i];
              s++;
            }
          }
          buffer.append(dataInput, 0, s);
        super.insertString(offs, new String(buffer), a);
        }
      };
      return filterLetter;
    }
  }
